﻿#include "FITKMeshGenInterface.h"
#include "FITKAbstractMeshSizeInfoGenerator.h"
#include "FITKRegionMeshSize.h"
#include "FITKGeometryMeshSize.h"
#include "FITKRegionGeometryRefine.h"
#include "FITKAbstractMesherDriver.h"
#include "FITKAbstractMeshProcessor.h"
#include "FITKAbstractGeometryMeshSizeGenerator.h"
#include "FITKZonePoints.h"
#include "FITKAbstractMeshAlgorithmInfoGenerator.h"
#include "FITKGlobalMeshGenerateAlgorithmInfo.h"

namespace Interface
{
    //静态变量初始化
    QMutex FITKMeshGenInterface::m_mutex;
    FITKMeshGenInterface* FITKMeshGenInterface::_instance = nullptr;

    FITKGlobalMeshSizeInfo* FITKMeshGenInterface::getGlobalMeshSizeInfo()
    {
        //全局尺寸为空则创建
        if (_globalSize == nullptr && _meshSizeGenor != nullptr)
        {
            _globalSize = _meshSizeGenor->generateGlobalMeshSizeInfo();
        }
        //返回全局尺寸
        return _globalSize;
    }

    void FITKMeshGenInterface::regMeshSizeGenerator(FITKAbstractMeshSizeInfoGenerator* genator)
    {
        _meshSizeGenor = genator;
    }

    FITKGlobalMeshGenerateAlgorithmInfo* FITKMeshGenInterface::getGlobalMeshGenerateAlgorithmInfo()
    {
        //全局网格算法信息为空则创建
        if (_globalMeshAlgorithm == nullptr && _meshAlgorithmGenor != nullptr)
        {
            _globalMeshAlgorithm = _meshAlgorithmGenor->generateGlobalMeshAlgorithmInfo();
        }
        //返回全局尺寸
        return _globalMeshAlgorithm;
    }

    void FITKMeshGenInterface::regMeshAlgorithmGenerator(FITKAbstractMeshAlgorithmInfoGenerator* genator)
    {
        _meshAlgorithmGenor = genator;
    }

    Interface::FITKAbstractMeshSizeInfoGenerator* FITKMeshGenInterface::getMeshSizeGenerator()
    {
        return _meshSizeGenor;
    }

    void FITKMeshGenInterface::regGeometryMeshSizeGenerator(FITKAbstractGeometryMeshSizeGenerator* genator)
    {
        _geoMeshSizeGenor = genator;
    }

    FITKAbstractGeometryMeshSizeGenerator* FITKMeshGenInterface::getGeometryMeshSizeGenerator()
    {
        return _geoMeshSizeGenor;
    }

    void FITKMeshGenInterface::regMesherDriver(FITKAbstractMesherDriver* driver)
    {
        _mesherDriver = driver;
    }

    Interface::FITKAbstractMesherDriver* FITKMeshGenInterface::getMesherDriver()
    {
        return _mesherDriver;
    }

    void FITKMeshGenInterface::regMeshProcessor(FITKAbstractMeshProcessor* p)
    {
        _meshProcessor = p;
    }

    Interface::FITKAbstractMeshProcessor* FITKMeshGenInterface::getMeshProcessor()
    {
        return _meshProcessor;
    }

    void FITKMeshGenInterface::regMeshProcessorFASTSIM(FITKAbstractMeshProcessor* p)
    {
        _meshProcessorFASTSIM= p;
    }

    Interface::FITKAbstractMeshProcessor* FITKMeshGenInterface::getMeshProcessorFASTSIM()
    {
        return _meshProcessorFASTSIM;
    }

    FITKRegionMeshSizeManager* FITKMeshGenInterface::getRegionMeshSizeMgr()
    {
        return _regionMeshMgr;
    }

    FITKGeometryMeshSizeManager* FITKMeshGenInterface::getGeometryMeshSizeManager()
    {
        return _geoMeshSizeMgr;
    }

    Interface::FITKZonePointManager* FITKMeshGenInterface::getZonePointManager()
    {
        return _zonePointsManager;
    }

    FITKRegionGeometryRefineManager* FITKMeshGenInterface::getRegionGeometryRefineManager()
    {
        return _regionGeoRefineMgr;
    }

    void FITKMeshGenInterface::initialize()
    {
        //创建对象
        _regionMeshMgr = new FITKRegionMeshSizeManager;
        _geoMeshSizeMgr = new FITKGeometryMeshSizeManager;
        _zonePointsManager = new FITKZonePointManager;
        _regionGeoRefineMgr = new FITKRegionGeometryRefineManager;
    }

    void FITKMeshGenInterface::finalize()
    {
        //释放内存
        if (_meshSizeGenor) delete _meshSizeGenor;
         _meshSizeGenor = nullptr;
        if (_regionMeshMgr) delete _regionMeshMgr;
        _regionMeshMgr = nullptr;
        if (_meshAlgorithmGenor) delete _meshAlgorithmGenor;
        _meshAlgorithmGenor = nullptr;
        if (_globalMeshAlgorithm) delete _globalMeshAlgorithm;
        _globalMeshAlgorithm = nullptr;
        if (_globalSize)  delete _globalSize;
        _globalSize = nullptr;
        if (_mesherDriver) delete _mesherDriver;
        _mesherDriver = nullptr;
        if (_meshProcessor) delete _meshProcessor;
        _meshProcessor = nullptr;
        if (_geoMeshSizeMgr) delete _geoMeshSizeMgr;
        _geoMeshSizeMgr = nullptr;
        if (_geoMeshSizeGenor) delete _geoMeshSizeGenor;
        _geoMeshSizeGenor = nullptr;
        if (_zonePointsManager) delete _zonePointsManager;
        _zonePointsManager = nullptr;
        if (_regionGeoRefineMgr) delete _regionGeoRefineMgr;
        _regionGeoRefineMgr = nullptr;
    }

    FITKMeshGenInterfaceDeleator::~FITKMeshGenInterfaceDeleator()
    {
        FITKMeshGenInterface* interface = FITKMeshGenInterface::getInstance();
        delete interface;
        FITKMeshGenInterface::_instance = nullptr;
    }


    static FITKMeshGenInterfaceDeleator __DELEATOR__;
}




